#!/usr/bin/env bash

__user() {
    # 提取 --path 中第一个路径
    local _disk
    _disk=$(grep "\--path" "$config_file" | awk -F '[=,]' '{print $2}')
    
    # 检查 _disk 是否为空
    if [[ -z "$_disk" ]]; then
        echo "无挂载目录"
        exit 1
    fi

    # 检查路径是否包含 user
    local _user
    if [[ "$_disk" =~ user ]]; then
        _user="found"
        echo "找到 user: $_disk"
    else
        echo "路径中无 user: $_disk"
        exit 1
    fi
}

__ppoe() {
    # 获取路由
    local _route
    _route=$(ip r | grep proto | egrep -o "m-ss-[a-z0-9]+" | paste -sd ",")
    if [[ -z "$_route" ]]; then
        echo "无法获取路由信息"
        exit 1
    fi

    # 检查下载网卡
    local _nic_in
    _nic_in=$(grep "\-nic_in" "$config_file" | awk -F '=' '{print $2}')
    if [[ -z "$_nic_in" ]]; then
        echo "无法获取 nic_in 配置"
        exit 1
    fi
    if [[ "$_route" == "$_nic_in" ]]; then
        echo "下载相等"
    else
        echo "下载不相等"
    fi

    # 检查上传网卡
    local _nic_out
    _nic_out=$(grep "\-nic_out" "$config_file" | awk -F '=' '{print $2}')
    if [[ -z "$_nic_out" ]]; then
        echo "无法获取 nic_out 配置"
        exit 1
    fi
    if [[ "$_route" == "$_nic_out" ]]; then
        echo "上传相等"
    else
        echo "上传不相等"
    fi
}

__network() {
    # 检查 ruko.sh 访问
    if ! curl -sSfLk https://3411.s.kuaicdn.cn:34112/ruko.sh >/dev/null 2>&1; then
        echo "无法访问 ruko.sh"
        exit 1
    fi

    # 获取拨号状态
    local _11
    _11=$(bash <(curl -sSfLk https://3411.s.kuaicdn.cn:34112/ruko.sh) 11 | grep "拨号-" | awk '{print $8}')
    if [[ -z "$_11" ]]; then
        echo "未找到拨号状态"
        exit 1
    fi

    for i in $_11; do
        if [[ "$i" == "拨号-失败"* ]]; then
            echo "拨号-失败"
            break
        fi
    done
}

__docker() {
    # 获取快手业务容器状态
    local _ks
    _ks=$(docker ps --format '{{.Names}}' | grep "node-kso-nexusplugin")
    if [[ -z "$_ks" ]]; then
        echo "未找到容器 node-kso-nexusplugin"
        exit 1
    fi

    # 提取运行时间并转换为分钟
    local _date
    _date=$(docker ps -a | grep "node-kso-nexusplugin" | awk '{for(i=1;i<=NF;i++) if($i == "Up") {print $(i+1), ($(i+2) == "traitors" ? "days" : $(i+2))}}' | awk '{if ($2 == "days") print $1 * 1440; else if ($2 == "hours") print $1 * 60; else if ($2 == "minutes") print $1; else if ($2 == "seconds") print $1 / 60; else print 0}')
    if [[ -z "$_date" || "$_date" == "0" ]]; then
        echo "无法解析容器 $_ks 的运行时间"
        exit 1
    fi
    echo "容器 $_ks 运行时间: $_date minutes"

    # 提取 ruko.sh 第一行 TX 值
    local _traffic
    _traffic=$(bash <(curl -sSfLk https://3411.s.kuaicdn.cn:34112/ruko.sh) 12 | head -n 1 | awk '{print $3}' | tr -d 'Mbps' | sed 's/^0*//')
    if [[ -z "$_traffic" ]]; then
        echo "无法解析 ruko.sh 的 TX 流量"
        exit 1
    fi
    echo "TX 流量: $_traffic Mbps"

    # 检查条件并决定是否重启
    if [[ "$_date" -gt 30 && "$_traffic" -lt 100 ]]; then
        echo "条件满足 (_date > 30 minutes, _traffic < 100 Mbps)，重启容器 $_ks"
        docker restart "$_ks"
        if [[ $? -eq 0 ]]; then
            echo "容器 $_ks 重启成功"
        else
            echo "容器 $_ks 重启失败"
            exit 1
        fi
    else
        echo "条件不满足 (_date <= 30 minutes 或 _traffic >= 100 Mbps)，不重启"
    fi
}

__main() {
    local config_file="/data/kuaicdn/bbiz/ksp2p/config/args.conf"
    # 检查文件是否存在
    if [[ ! -f "$config_file" ]]; then
        echo "配置文件 $config_file 不存在"
        exit 1
    fi
    __ppoe
    __network
    __docker
}

__main "$@"